package com.example.martinrgb.giftcard.slice;

import com.example.martinrgb.giftcard.ResourceTable;
import com.facebook.rebound.*;
import ohos.aafwk.ability.AbilitySlice;
import ohos.aafwk.content.Intent;
import ohos.agp.animation.Animator;
import ohos.agp.animation.AnimatorValue;
import ohos.agp.components.Button;
import ohos.agp.components.Component;
import ohos.agp.components.Image;
import ohos.agp.components.element.FrameAnimationElement;
import ohos.eventhandler.EventHandler;
import ohos.eventhandler.EventRunner;

public class MainAbilitySlice extends AbilitySlice {

    private static final SpringConfig config = SpringConfig.fromBouncinessAndSpeed(0, 2);
    private static final SpringConfig config2 = SpringConfig.fromOrigamiTensionAndFriction(5, 10);
    private SpringSystem mSpringSystem;
    private Spring mSpring;
    private Spring mSpring1;
    private Spring mSpring2;
    private Spring mSpring3;
    private Spring mSpring4;
    private Spring mSpring5;
    private Image ml1;
    private Image ml2;
    private Image ml3;
    private Image ml4;
    private Image ml5;
    private Image mpricelabel;
    private Button mframebutton;

    private final SpringChain mSpringChain = SpringChain.create();

    private EventHandler eventHandler = new EventHandler(EventRunner.getMainEventRunner());

    @Override
    public void onStart(Intent intent) {
        super.onStart(intent);
        super.setUIContent(ResourceTable.Layout_ability_main);

        eventHandler.postTask(() -> {
            AnimatorValue animator = createTranslationAnim(findComponentById(ResourceTable.Id_wave1), 0f, 250f);
            animator.start();
        }, 50);
        eventHandler.postTask(() -> {
            AnimatorValue animator = createTranslationAnim(findComponentById(ResourceTable.Id_wave2), 0f, 200f);
            animator.start();
        }, 50);

        // 为了延迟
        // 文字
        ml1 = (Image) findComponentById(ResourceTable.Id_l1);
        ml2 = (Image) findComponentById(ResourceTable.Id_l2);
        ml3 = (Image) findComponentById(ResourceTable.Id_l3);
        ml4 = (Image) findComponentById(ResourceTable.Id_l4);
        ml5 = (Image) findComponentById(ResourceTable.Id_l5);
        mpricelabel = (Image) findComponentById(ResourceTable.Id_pricelabel);
        mframebutton = (Button) findComponentById(ResourceTable.Id_framebutton);


        mSpringSystem = SpringSystem.create();

        mSpring = mSpringSystem.createSpring();
        mSpring1 = mSpringSystem.createSpring();
        mSpring2 = mSpringSystem.createSpring();
        mSpring3 = mSpringSystem.createSpring();
        mSpring4 = mSpringSystem.createSpring();
        mSpring5 = mSpringSystem.createSpring();

        //mspring随便设置的
        mSpring.addListener(new ViewSpringListener(mpricelabel));
        mSpring1.addListener(new ViewSpringListener(ml1));
        mSpring2.addListener(new ViewSpringListener(ml2));
        mSpring3.addListener(new ViewSpringListener(ml3));
        mSpring4.addListener(new ViewSpringListener(ml4));
        mSpring5.addListener(new ViewSpringListener(ml5));


        mSpring.setSpringConfig(config);
        mSpring1.setSpringConfig(config);
        mSpring2.setSpringConfig(config);
        mSpring3.setSpringConfig(config);
        mSpring4.setSpringConfig(config);
        mSpring5.setSpringConfig(config);

        //设置起始位置
        Component input = findComponentById(ResourceTable.Id_input);
        input.setLayoutRefreshedListener(component -> {
            input.setTranslationX(1085);
            input.setLayoutRefreshedListener(null);
        });

        Component checkout = findComponentById(ResourceTable.Id_checkout);
        checkout.setLayoutRefreshedListener(component -> {
            checkout.setTranslationX(1565);
            checkout.setLayoutRefreshedListener(null);
        });

        //帧动画&延迟mSpring数值
        mframebutton.setClickedListener(v -> {
            FrameAnimationElement ad = (FrameAnimationElement) mframebutton.getBackgroundElement();
            ad.start();
            eventHandler.postTask(() -> findComponentById(ResourceTable.Id_framebutton).setClickedListener(null), 42 * 38);

            //也可以放进去，但是文本异常
            mSpring.setEndValue(0f);
            mSpring1.setEndValue(1f);
            mSpring2.setEndValue(1f);
            mSpring3.setEndValue(1f);
            mSpring4.setEndValue(1f);
            mSpring5.setEndValue(1f);

        });
        Component l1 = findComponentById(ResourceTable.Id_l1);
        l1.setLayoutRefreshedListener(component -> {
            l1.setTranslationX(1000);
            l1.setLayoutRefreshedListener(null);
        });
        Component l2 = findComponentById(ResourceTable.Id_l2);
        l2.setLayoutRefreshedListener(component -> {
            l2.setTranslationX(1000);
            l2.setLayoutRefreshedListener(null);
        });

        Component l3 = findComponentById(ResourceTable.Id_l3);
        l3.setLayoutRefreshedListener(component -> {
            l3.setTranslationX(1000);
            l3.setLayoutRefreshedListener(null);
        });

        Component l4 = findComponentById(ResourceTable.Id_l4);
        l4.setLayoutRefreshedListener(component -> {
            l4.setTranslationX(1000);
            l4.setLayoutRefreshedListener(null);
        });

        Component l5 = findComponentById(ResourceTable.Id_l5);
        l5.setLayoutRefreshedListener(component -> {
            l5.setTranslationX(1000);
            l5.setLayoutRefreshedListener(null);
        });
    }

    public class ViewSpringListener implements SpringListener {
        Component _view;

        ViewSpringListener(Component view) {
            this._view = view;
        }

        @Override
        public void onSpringUpdate(Spring spring) {

            eventHandler.postTask(() -> {

                mSpring.setEndValue(1);
                float value = (float) mSpring.getCurrentValue();

                float topscale =
                        (float) SpringUtil.mapValueFromRangeToRange(value, 0, 1, 1, 0.88);
                topscale = Math.max(topscale, 0);
                findComponentById(ResourceTable.Id_topthings).setScaleX(topscale);
                findComponentById(ResourceTable.Id_topthings).setScaleY(topscale);
                findComponentById(ResourceTable.Id_wave1).setScaleX(topscale);
                findComponentById(ResourceTable.Id_wave1).setScaleY(topscale);
                findComponentById(ResourceTable.Id_wave2).setScaleX(topscale);
                findComponentById(ResourceTable.Id_wave2).setScaleY(topscale);

                float topy = (float) SpringUtil.mapValueFromRangeToRange(value, 0, 1, 0, 500);
                findComponentById(ResourceTable.Id_topthings).setTranslationY(topy);
                findComponentById(ResourceTable.Id_wave1).setTranslationY(topy);
                findComponentById(ResourceTable.Id_wave2).setTranslationY(topy);

                float buttonx = (float) SpringUtil.mapValueFromRangeToRange(value, 0, 1, 0, -680);
                findComponentById(ResourceTable.Id_framebutton).setTranslationX(buttonx);

                float pricex = (float) SpringUtil.mapValueFromRangeToRange(value, 0, 1, 0, -680);
                findComponentById(ResourceTable.Id_pricelabel).setTranslationX(pricex);

                float checkoutx = (float) SpringUtil.mapValueFromRangeToRange(value, 0, 1, 800, 0);
                findComponentById(ResourceTable.Id_checkout).setTranslationX(checkoutx);

                float inputx = (float) SpringUtil.mapValueFromRangeToRange(value, 0, 1, 800, 0);
                findComponentById(ResourceTable.Id_input).setTranslationX(inputx);

            }, 42 * 38);

            eventHandler.postTask(() -> {

                mSpring1.setEndValue(0);
                float value1 = (float) mSpring1.getCurrentValue();

                float l1x = (float) SpringUtil.mapValueFromRangeToRange(value1, 0, 1, 0, 1000);
                findComponentById(ResourceTable.Id_l1).setTranslationX(l1x);

            }, 42 * 38 - 100);

            eventHandler.postTask(() -> {

                mSpring2.setEndValue(0);
                float value2 = (float) mSpring2.getCurrentValue();
                float l2x = (float) SpringUtil.mapValueFromRangeToRange(value2, 0, 1, 0, 1000);
                findComponentById(ResourceTable.Id_l2).setTranslationX(l2x);

            }, 42 * 38 - 50);

            eventHandler.postTask(() -> {
                mSpring3.setEndValue(0);
                float value3 = (float) mSpring3.getCurrentValue();
                float l3x = (float) SpringUtil.mapValueFromRangeToRange(value3, 0, 1, 0, 1000);
                findComponentById(ResourceTable.Id_l3).setTranslationX(l3x);

            }, 42 * 38);

            eventHandler.postTask(() -> {
                mSpring4.setEndValue(0);
                float value4 = (float) mSpring4.getCurrentValue();
                float l4x = (float) SpringUtil.mapValueFromRangeToRange(value4, 0, 1, 0, 1000);
                findComponentById(ResourceTable.Id_l4).setTranslationX(l4x);

            }, 42 * 38 + 50);

            eventHandler.postTask(() -> {
                mSpring5.setEndValue(0);
                float value5 = (float) mSpring5.getCurrentValue();
                float l5x = (float) SpringUtil.mapValueFromRangeToRange(value5, 0, 1, 0, 1000);
                findComponentById(ResourceTable.Id_l5).setTranslationX(l5x);

            }, 42 * 38 + 100);
        }

        @Override
        public void onSpringAtRest(Spring spring) {
        }

        @Override
        public void onSpringActivate(Spring spring) {
        }

        @Override
        public void onSpringEndStateChange(Spring spring) {
        }
    }

    private AnimatorValue createTranslationAnim(Component view, float start, float end) {
        AnimatorValue va = new AnimatorValue();
        va.setDuration(15000);
        va.setLoopedCount(Animator.INFINITE);
        va.setCurveType(Animator.CurveType.LINEAR);
        va.setValueUpdateListener((animatorValue, v) -> {
            float result = (float) calAnimValue(start, end, v);
            view.setTranslationX(result);
        });
        return va;
    }

    private double calAnimValue(double start, double end, double curr) {
        return start + (end - start) * curr;
    }

}
